package sedev.shopping.data;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import sedev.shopping.entities.DonViGia;
import sedev.shopping.entities.ListThongTinShop;
import sedev.shopping.entities.ThongTinShop;
import sedev.shopping.util.Connector;
import sedev.shopping.util.HibernateUtil;
import sedev.shopping.util.Paging;

public class ThongTinShopDAO {
	public ThongTinShopDAO(){
		
	}
	
	private ThongTinShop initData(ResultSet result_set){
		ThongTinShop data = new ThongTinShop();
		try{
			data.setId(result_set.getLong(ThongTinShop.Properties.id.name()));
        }catch(SQLException sqle){
        	data.setId(Long.valueOf(0));
        }    
        try{
        	data.setGroupId(result_set.getLong(ThongTinShop.Properties.group_id.name()));
        }catch(SQLException sqle){
        	data.setGroupId(Long.valueOf(0));
        } 
        try{
        	data.setUserId(result_set.getLong(ThongTinShop.Properties.user_id.name()));
        }catch(SQLException sqle){
        	data.setUserId(Long.valueOf(0));
        }        
        try{
        	data.setTenShop(result_set.getString(ThongTinShop.Properties.ten_shop.name()));
        }catch(SQLException sqle){
        	data.setTenShop("");
        }
        try{
        	data.setSlogan(result_set.getString(ThongTinShop.Properties.slogan.name()));
        }catch(SQLException sqle){
        	data.setSlogan("");
        }
        try{
        	data.setDiaChi(result_set.getString(ThongTinShop.Properties.dia_chi.name()));
        }catch(SQLException sqle){
        	data.setDiaChi("");
        }
        try{
        	data.setPhamViId(result_set.getInt(ThongTinShop.Properties.pham_vi_id.name()));
        }catch(SQLException sqle){
        	data.setPhamViId(0);
        }
        try{
        	data.setSoSanPhamTrongNgay(result_set.getInt(ThongTinShop.Properties.so_san_pham_trong_ngay.name()));
        }catch(SQLException sqle){
        	data.setSoSanPhamTrongNgay(0);
        }
        try{
        	data.setSoSanPhamVipTrongNgay(result_set.getInt(ThongTinShop.Properties.so_san_pham_vip_trong_ngay.name()));
        }catch(SQLException sqle){
        	data.setSoSanPhamVipTrongNgay(0);
        }
        try{
        	data.setChuShop(result_set.getString(ThongTinShop.Properties.chu_shop.name()));
        }catch(SQLException sqle){
        	data.setChuShop("");
        }
        try{
        	data.setEmail(result_set.getString(ThongTinShop.Properties.email.name()));
        }catch(SQLException sqle){
        	data.setEmail("");
        }
        try{
        	data.setPhone(result_set.getString(ThongTinShop.Properties.phone.name()));
        }catch(SQLException sqle){
        	data.setPhone("");
        }
        try{
        	data.setNgayDang(result_set.getLong(ThongTinShop.Properties.ngay_dang.name()));
        }catch(SQLException sqle){
        	data.setNgayDang(Long.valueOf(0));
        }
        try{
        	data.setHienThi(result_set.getBoolean(ThongTinShop.Properties.hien_thi.name()));
        }catch(SQLException sqle){
        	data.setHienThi(false);
        }
		return data;
	}

	public boolean addData(ThongTinShop data){
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();	
			session.save(data);			
			transaction.commit();
		}catch(HibernateException ex){
			if(transaction != null){
				transaction.rollback();				
			}
			return false;		
		}
		return true;
	}
	
	public boolean updateData(ThongTinShop data){
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();	
			session.update(data);			
			transaction.commit();
		}catch(HibernateException ex){
			if(transaction != null){
				transaction.rollback();				
			}
			return false;		
		}
		return true;
	}
	
	public boolean removeData(Long id){
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;		
		try{
			transaction = session.beginTransaction();			
			ThongTinShop data = new ThongTinShop();
			data.setId(id);
			session.delete(data);
			transaction.commit();
		}catch(HibernateException ex){
			if(transaction != null){
				transaction.rollback();				
			}
			return false;		
		}		
		return true;
	}
	
	public ThongTinShop getData(Long id){
		ThongTinShop data = new ThongTinShop();		
		Session session = HibernateUtil.getSession().openSession();		
		try{
			session.load(data,id);		
		}catch(HibernateException ex){
			data = null;
		}
		return data;
	}
	
	public ThongTinShop getData(String condition){
		ThongTinShop data = new ThongTinShop();		
		Connector conn = Connector.getInstance();
		StringBuilder sql = new StringBuilder();
		sql.append("select ").append("*");
		sql.append(" from ").append(ThongTinShop.Table);
		sql.append(" where ").append(condition);
		ResultSet rs = conn.executeQuery(sql.toString());
		try{
            if(rs != null && rs.next()){                                
            	data = this.initData(rs);
            }else{
            	data = null;
            }
        }catch(SQLException sqle){
        	data = null;
        }
		return data;
	}
	
	public ListThongTinShop getListData(String field, String condition, String order, Paging paging){
		ListThongTinShop listdata = new ListThongTinShop();			
		Connector conn = Connector.getInstance();
		
		StringBuilder sql = new StringBuilder();
		sql.append("select ").append(field);
		sql.append(" from ").append(ThongTinShop.Table);
		sql.append(" where ").append(condition);
		if(order!=null && !order.equals("")){
			sql.append(" order by ").append(order);
		}
		if(paging.getPageSize() > 0){
			paging.setTotalRecord(conn.getTotalRecord(DonViGia.Table, condition));
            paging.checkPage();
            Long offset = paging.getCurrent().longValue() - 1;
            offset = offset * paging.getPageSize();
            sql.append(" limit ").append(offset).append(",").append(paging.getPageSize());
        }
		
		ResultSet rs = conn.executeQuery(sql.toString());		
		try{
            if(rs != null){
                while(rs.next()){                   
                   listdata.add(this.initData(rs));
                }
            }
        }catch(SQLException sqle){
        	listdata.clear();
        }
		return listdata;
	}
}
